﻿@namespace BootstrapBlazor.Components
@typeparam TItem
@inherits BootstrapComponentBase

<div @attributes="@AdditionalAttributes" class="@ClassString">
    @if (HeaderTemplate != null)
    {
        <div class="listview-header">
            @HeaderTemplate
        </div>
    }
    <div class="@BodyClassString">
        @if (BodyTemplate != null)
        {
            if (GroupName == null)
            {
                foreach (var item in Rows)
                {
                    <div class="listview-item" @onclick="@(e => OnClick(item))">
                        @BodyTemplate.Invoke(item)
                    </div>
                }
            }
            else if(Collapsable)
            {
                <Collapse IsAccordion="IsAccordion" OnCollapseChanged="OnCollapseChanged!">
                    <CollapseItems>
                        @{
                            var index = 0;
                            foreach (var key in Rows.GroupBy(GroupName).OrderBy(k => k.Key))
                            {
                                <CollapseItem @key="@key" Text="@key.Key?.ToString()" IsCollapsed="IsCollapsed(index++, key.Key)">
                                    @foreach (var item in key)
                                    {
                                        <div class="listview-item" @onclick="@(e => OnClick(item))">
                                            @BodyTemplate.Invoke(item)
                                        </div>
                                    }
                                </CollapseItem>
                            }
                        }
                    </CollapseItems>
                </Collapse>
            }
            else
            {
                foreach (var key in Rows.GroupBy(GroupName).OrderBy(k => k.Key))
                {
                    <div class="accordion-item">
                        <div class="accordion-header">@key.Key</div>
                        <div class="accordion-body">
                            @foreach (var item in key)
                            {
                                <div class="listview-item" @onclick="@(e => OnClick(item))">
                                    @BodyTemplate.Invoke(item)
                                </div>
                            }
                        </div>
                    </div>
                }
            }
        }
    </div>
    @if (FooterTemplate != null || Pageable)
    {
        <div class="listview-footer">
            @if (FooterTemplate != null)
            {
                @FooterTemplate
            }
            else if (Pageable)
            {
                <Pagination PageCount="@PageCount" PageIndex="@PageIndex" OnPageLinkClick="@OnPageLinkClick"></Pagination>
            }
        </div>
    }
</div>
